<template>
  <MainSlot v-loading="loading">
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search"
        @isUnfold="(v) => { unfoldFlag = v }">
      </SearchForm>
    </template>

    <template v-slot:table>
      <TableData :config="table_config" :tableThead="tableThead" :dataList="dataList" :isSelect="false"
        :loading="loading" @selection-change="selectionChange">
        <template v-slot:checkStage="{ row }">
          {{ radioObj[row.checkStageId] }}
        </template>
        <template v-slot:technicianResult="{ row }">
          {{ row.technicianResult?row.technicianResult:'暂未结论'}}
        </template>
        <template v-slot:technicianStatus="{ row }">
          {{ row.technicianStatus?row.technicianStatus:'暂未结论'}}
        </template>
        <template v-slot:totalSocre="{ row }">
          {{ row.totalSocre?row.totalSocre:'0'}}分
        </template>
      </TableData>

    </template>

    <template v-slot:pagination>
      <Pagination @size-Change="handleSizeChange" :size="size"   @current-change="handleCurrentChange" :page="page" :total="total">
      </Pagination>
    </template>

    <template v-slot:dialogModule>
      <AddEdit :title="'查看质检单'" @closeDialog="closeDialog" :addEditFlag="addEditFlag" :dialogVisible="dialogVisible"
        :data="formData">
      </AddEdit>
    </template>
  </MainSlot>
</template>

<script>
import MainSlot from "@/components/MainSlot";
import Pagination from '@/components/Pagination';
import SearchForm from '@/components/SearchForm';
import TableData from '@/components/TableData';
import BtnSlot from '@/components/BtnSlot';
import EnumSlot from '@/components/EnumSlot';
import AddEdit from './components/addEdit.vue';
import inspectionManagement from '@/api/inspectionManagement/inspectionManagement';
import inspectionPhase from '@/api/basicInformation/inspectionPhase.js';
import varietyManagement from '@/api/basicInformation/varietyManagement';
export default {
  components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot, EnumSlot, AddEdit },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      dialogVisible: false,
      addEditFlag: 'add',
      selectList: [],
      formData: {},
      page: 1,
      size:10,
      selectIndex: 0,
      total: 1000,
      btnList: ['export'],
      dataList: [],
      tableThead: [
        { isShow: true, label: '供应商', prop: 'supplier'},
        { isShow: true, label: '检测阶段', slotName: 'checkStage' },
        { isShow: true, label: '检验品种', prop: 'plantVarietyName' },
        { isShow: true, label: '检验人', prop: 'staffName' },
        { isShow: true, label: '技术员结论', slotName: 'technicianResult' },
        { isShow: true, label: '技术员评定结果', slotName: 'technicianStatus' },
        { isShow: true, label: '检验分数', slotName: 'totalSocre' },
        { isShow: true, label: '检验时间', prop: 'checkTime' ,minWidth: '150px'}
      ],
      radioList: [],
      varietyList: [],
      radioObj: {},
      searchFormData: {},
      // 配置项
      table_config: {
        isCheckBox: true,
        isOperation: true,
        // 表格操作列
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '结果判定',
              type: 'primary',
              flag: 'edit',
              handleRow: this.handleRow
            }
          ]
        },
      },
      formItemList: [
        {
          label: '检验阶段',
          type: 'select',
          selectOptions: [],
          param: 'checkStageId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '检验品种',
          type: 'select',
          selectOptions: [],
          param: 'plantVarietyId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '检验人',
          type: 'select',
          selectOptions: [],
          param: 'staffName',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '检验日期',
          type: 'datetimerange',
          param: 'dateTime',
          multiple: false,
          isShow: true
        }
      ]
    }
  },
  computed: {
    person() {
      return this.$store.state.initData.person || [];
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      let res = await inspectionPhase.queryByCondition({});
      this.radioList = res.data || []
      this.radioList.forEach(item => {
        this.radioObj[item.checkStageId] = item.checkStageName
      })
      //获取品种；
      let { data: varietyList } = await varietyManagement.queryByCondition({})
      this.varietyList = varietyList || [];
      this.queryPageByCondition();
      this.setFormOptions();
    },
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = []
      list.forEach((item, index) => {
        if (item.param.indexOf('checkStageId') !== -1) {
          console.log(this.facilityList)
          let selectOptions = this.radioList.map(data => {
            return {
              text: data.checkStageName, value: data.checkStageId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
        if (item.param.indexOf('plantVarietyId') !== -1) {
          let selectOptions = this.varietyList.map(data => {
            return {
              text: data.plantVarietyName, value: data.plantVarietyId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
        if (item.param == 'staffName') {
          let selectOptions = this.person.map(data => {
            return {
              text: data.person.name, value: data.person.name
            }
          })
          item.defaultSelect = this.searchFormData[item.param]
          item.selectOptions = selectOptions;
        };
      });
      this.$nextTick(() => {
        this.$set(this, 'formItemList', list)
        console.log(this.formItemList)
      })
    },
    delList(deleteList = [], key = 'checkScopeTypeId') {
      let list = []
      deleteList.forEach(item => {
        list.push(item[key])
      })
      console.log(list)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await inspectionManagement.delList(list);
        if (res.code == 1) {
          this.$message.success(res.msg || '删除成功');
          this.queryPageByCondition();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData,
          status: 'MANAGER_EVALUATION'
        }
      }
      inspectionManagement.queryPageByCondition(params).then(res => {
        if (res.code == 1) {
          this.dataList = res.data.content || [];
          this.total = res.data.totalElements;
          if (res.data.totalPages && this.page > res.data.totalPages) {
            this.page = res.data.totalPages;
            this.queryPageByCondition();
          }
        } else {
          this.$message.warning(res.msg || '分页查询失败')
        }
      }).catch(err => {
        console.log(err)
      })
    },
    search(data) {
      this.page = 1;
      let endData = null;
      let startData = null;
      if (data.dateTime && data.dateTime.length == 2) {
        startData = `${data.dateTime[0]} 00:00:00`;
        endData = `${data.dateTime[1]} 23:59:59`;
      }
      this.searchFormData = {
        ...this.searchFormData,
        ...data,
        endTime: endData,
        startTime: startData
      };
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryPageByCondition();
    },
    // 操作方法
    handleRow(rowData, item) {
      if (item.flag == 'edit') {
        this.dialogVisible = true;
        this.formData = JSON.parse(JSON.stringify(rowData))
      }
      this.addEditFlag = item.flag
    },
    // 多选方法
    selectionChange(val) {
      this.selectList = val || []
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.dialogVisible = true
      } else if (flag == 'delete') {
        if (this.selectList.length > 0) {
          this.delList(this.selectList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag
    },
    //关闭弹窗
    closeDialog() {
      this.formData = {};
      this.dialogVisible = false;
      this.queryPageByCondition();
    },
  }
};
</script>

<style lang="scss" scoped></style>